package com.theme.passport.controller.sys;

import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.constant.ThemeStatusEnum;
import com.theme.passport.entity.theme.AlbumProductEntity;
import com.theme.passport.service.theme.AlbumProductService;
import com.theme.passport.service.theme.ThemeService;
import com.theme.passport.service.theme.WallpaperService;
import com.theme.passport.service.theme.WidgeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 专题产品内容
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2023-02-28 10:01:58
 */
@RestController
@RequestMapping("sys/album-product")
public class SysAlbumProductController {

    @Autowired
    private ThemeService themeService;

    @Autowired
    private WallpaperService wallpaperService;

    @Autowired
    private WidgeService widgeService;

    @Autowired
    private AlbumProductService albumProductService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:album:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = albumProductService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:album:info")
    public R info(@PathVariable("id") Long id){
		AlbumProductEntity albumProduct = albumProductService.getById(id);

        return R.ok().put("albumProduct", albumProduct);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:album:save")
    public R save(@RequestBody AlbumProductEntity albumProduct){
        albumProduct.setCreated(new Date());
        albumProductService.save(albumProduct);

        return R.ok();
    }

    /**
     * 修改
     */
//    @RequestMapping("/update")
//    @RequiresPermissions("album:album-product:update")
//    public R update(@RequestBody AlbumProductEntity albumProduct){
//        albumProduct.setUpdated(new Date());
//        albumProductService.updateById(albumProduct);
//        return R.ok();
//    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:album:delete")
    public R delete(@RequestBody Long[] ids){
        albumProductService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 所有主题
     */
    @RequestMapping("/themeList")
    @RequiresPermissions("sys:album:list")
    public R themeList(@RequestParam Map<String, Object> params){
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        params.put("status",status +"");
        PageUtils page = themeService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 所有组件
     */
    @RequestMapping("/widgeList")
    @RequiresPermissions("sys:album:list")
    public R widgeList(@RequestParam Map<String, Object> params){
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        params.put("status",status +"");
        params.put("themeShow", false);
        PageUtils page = widgeService.queryPageByDistinctName(params);
        return R.ok().put("page", page);
    }

    /**
     * 所有壁纸
     */
    @RequestMapping("/wallpaperList")
    @RequiresPermissions("sys:album:list")
    public R wallpaperList(@RequestParam Map<String, Object> params){
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        params.put("status",status +"");
        PageUtils page = wallpaperService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 今日推荐主题
     */
    @RequestMapping("/selectedList")
    @RequiresPermissions("sys:album:list")
    public R selectedList(@RequestParam Map<String, Object> params){
        PageUtils page = albumProductService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:album:update")
    public R update(@RequestBody Long[] ids, @RequestParam("albumId") Long albumId){
        List<Long> objIds = Arrays.asList(ids);
        //先全部删除，然后重新插入：
        albumProductService.updateAlbumProduct(objIds, albumId);
        return R.ok();
    }

}
